package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.bean.RiderBean;
import com.project.mapper.RiderMapper;
import com.project.service.RiderService;
import com.project.util.codingUtils.StringUtil;
import com.project.vo.Infos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * 骑手实现类
 *
 * @author lyl
 * @since 2021-10-04
 */
@Service
public class RiderServiceImpl extends ServiceImpl<RiderMapper, RiderBean> implements RiderService {
    @Autowired
    RiderMapper riderMapper;

    @Override
    public RiderBean getRiderByCode(String code) {
        Boolean flag = StringUtil.verify(code);
        if (flag) {
            QueryWrapper qu = new QueryWrapper<>();
            qu.eq("code", code);
            RiderBean riderBean = riderMapper.selectOne(qu);
            return riderBean;
        }
        return null;
    }

    @Override
    public List<RiderBean> findAllRiderInfo() {
        return riderMapper.selectList(null);
    }

    @Override
    public Infos setRiderState(String code, String state) {
        QueryWrapper qu = new QueryWrapper();
        qu.eq("code", code);
        RiderBean riderBean = riderMapper.selectOne(qu);
        if (riderBean == null) {
            return new Infos(0, "修改失败");
        }
        riderBean.setState(state);
        int x = riderMapper.updateById(riderBean);

        if (x == 1) {
            return new Infos(1, "修改成功");
        }
        return new Infos(0, "修改失败");
    }


    @Override
    public RiderBean findRiderByCodeAndDate(String code, String date) {
        String time = LocalDate.now() + " " + "00:00:00";
        return riderMapper.findRiderByCodeAndDate(code, time, date);
    }

    @Override
    public RiderBean findRiderByIncome(String code, String startDate, String endDate) {

        RiderBean riderBean = riderMapper.findByIncome(code);
        if (riderMapper.findRiderByIncome(code, startDate, endDate) == null) {
            riderBean.setTodayIncome(0);
        } else {
            riderBean.setTodayIncome(riderMapper.findRiderByIncome(code, startDate, endDate));
        }
        return riderBean;
    }

    @Override
    public Infos updatePhone(String riderCode, String phone) {
        QueryWrapper qu = new QueryWrapper();
        qu.eq("code", riderCode);
        RiderBean riderBean = riderMapper.selectOne(qu);
        riderBean.setTel(phone);
        int x = riderMapper.updateById(riderBean);
        if (x == 1) {
            return new Infos(1, "修改成功");
        }
        return new Infos(0, "修改失败");
    }
}
